<template>
  <div class="adddish">
    <div class="from">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 序号 -->
        <el-form-item label="序号" prop="index">
          <el-input v-model="ruleForm.index"></el-input>
        </el-form-item>
        <!-- 名称 -->
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <!-- 价格 -->
        <el-form-item label="价格" prop="price">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <!-- 分类 -->
        <el-form-item label="分类" prop="serie">
          <el-select v-model="ruleForm.serie" placeholder="请选择菜品分类">
            <el-option
              v-for="(v, i) in typeList"
              :label="v.name"
              :value="v._id"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 是否上架 -->
        <el-form-item label="是否上架" prop="status">
          <el-switch v-model="ruleForm.status"></el-switch>
        </el-form-item>
        <!-- 图片url -->
        <el-form-item label="图片url" prop="img">
          <el-input v-model="ruleForm.img"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getType, AddDish } from "@/api/dish";
export default {
  data() {
    return {
      /* 菜品信息 */
      ruleForm: {
        name: "",
        index: "",
        status: false,
        serie: "",
        img: "",
        price: "",
      },
      /* 检验规则 */
      rules: {
        name: [{ required: true, message: "请输入菜品名称", trigger: "blur" }],
        price: [{ required: true, message: "请输入菜品价格", trigger: "blur" }],
        index: [
          { required: true, message: "请输入菜品序号", trigger: "change" },
        ],
        imgUrl: [
          {
            required: true,
            message: "请输入菜品图片url地址",
            trigger: "change",
          },
        ],
        type: [{ required: true, message: "请选择菜品分类", trigger: "blur" }],
      },
      // 菜品分类列表
      typeList: [],
    };
  },
  methods: {
    // 表单提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // 校验表单
        if (valid) {
          // 提交添加
          AddDish(this.ruleForm)
            .then((res) => {
              //提示成功
              this.$message({
                type: "success",
                message: res.msg,
              });
              //重置表单
              this.$refs["ruleForm"].resetFields();
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    // 获取菜品分类
    getType()
      .then((res) => {
        this.typeList = res.data;
      })
      .catch((err) => {
        this.$message.error("获取菜品分类失败！！");
      });
  },
};
</script>

<style scoped lang="scss">
.adddish {
  padding: 20px;
  .from {
    width: 40%;
  }
}
</style>